package com.wx.mall.operate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.wx.mall.operate.dto.SeckillActivitiesDto;
import com.wx.mall.operate.entity.SeckillActivities;
import com.wx.mall.operate.mapper.SeckillActivitiesMapper;
import com.wx.mall.operate.service.ISeckillActivitiesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author j
 * @since 2020-05-28
 */
@Service
public class SeckillActivitiesServiceImpl extends ServiceImpl<SeckillActivitiesMapper, SeckillActivities> implements ISeckillActivitiesService {

    @Override
    public List<SeckillActivities> findAllActivities(SeckillActivitiesDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<SeckillActivities> queryWrapper=new QueryWrapper<>();
        if (dto.getSort()==0){
            queryWrapper.orderByAsc("start_time").eq("activity_delete",1);
        };
        if (dto.getSort()==1){
            queryWrapper.orderByAsc("end_time").eq("activity_delete",1);
        };
        if (dto.getSort()==2){
            queryWrapper.orderByAsc("activity_status").eq("activity_delete",1);
        }
        List<SeckillActivities> list=this.list(queryWrapper);
        return list;
    }

    @Override
    public List<SeckillActivities> findActivityList(SeckillActivitiesDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<SeckillActivities> wrapper=new QueryWrapper<>();
        if (dto.getActivityName()!=null){
            wrapper.like("activity_name",dto.getActivityName()).eq("activity_delete",1);
        }
        if (dto.getSort()==0){
            wrapper.orderByAsc("start_time").eq("activity_delete",1);
        };
        if (dto.getSort()==1){
            wrapper.orderByAsc("end_time").eq("activity_delete",1);
        };
        if (dto.getSort()==2){
            wrapper.orderByAsc("activity_status").eq("activity_delete",1);
        }
        List<SeckillActivities> list=this.list(wrapper);
        return list;
    }

    @Override
    public int addActivityList(SeckillActivitiesDto dto) {
        Random random=new Random();
        int a=random.nextInt(3)+1;
        SeckillActivities seckillActivities=new SeckillActivities();
        seckillActivities.setId(dto.getId());
        seckillActivities.setActivityName(dto.getActivityName());
        seckillActivities.setActivityStatus(a);
        seckillActivities.setStartTime(dto.getStartTime());
        seckillActivities.setEndTime(dto.getEndTime());
        seckillActivities.setActivityType(dto.getActivityType());
        seckillActivities.setActivityDelete(1);
        boolean save = this.save(seckillActivities);
        return save?1:0;
    }

    @Override
    public int updateActivityList(SeckillActivitiesDto dto) {
        SeckillActivities seckillActivities1=new SeckillActivities();
        seckillActivities1.setId(dto.getId());
        seckillActivities1.setActivityName(dto.getActivityName());
        seckillActivities1.setStartTime(dto.getStartTime());
        seckillActivities1.setEndTime(dto.getEndTime());
        seckillActivities1.setActivityType(dto.getActivityType());
        boolean b = this.updateById(seckillActivities1);
        return b?1:0;
    }

    @Override
    public int delActivityList(Integer id) {
        List<SeckillActivities> list=new ArrayList<>();
        SeckillActivities seckillActivities=this.getById(id);
        seckillActivities.setActivityDelete(0);
        list.add(seckillActivities);
        boolean b = this.updateBatchById(list);
        return b?1:0;
    }
}
